using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using System.Collections;

namespace eHouse
{
    class IrrigationScheduleWindow:Window
    {
        private Window irrigationScheduleWindow;
        private Canvas canvas = new Canvas();        
       
        private ArrayList irrigation_rules;

        ArrayList rule_array = new ArrayList();
        ArrayList delete_array = new ArrayList();

        public IrrigationScheduleWindow()
        {
            irrigationScheduleWindow = new Window();
            irrigationScheduleWindow.Width = SystemMetrics.ScreenWidth;
            irrigationScheduleWindow.Height = SystemMetrics.ScreenHeight;

            irrigation_rules = IrrigationBL.GetInstance().GetRules();
            CreateWindow();
        }

        public void CreateWindow()
        {
            if (irrigation_rules.Count != 0)
            {
                int i = 0;
                foreach(Rule rule in irrigation_rules)
                {
                    if (rule != null)
                    {                        
                        string id = rule.ID;
                        string time = rule.Time;
                        bool state = (bool)rule.Body;
                        string on_off = "";
                        if (state)
                            on_off = "ON";
                        else
                            on_off = "OFF";

                        rule_array.Add(new Text(Resources.GetFont(Resources.FontResources.NinaB), "Rule "+(i+1)+": "+ time +" - "+on_off));
                        delete_array.Add(new SimpleButton(new Image(Resources.GetBitmap(Resources.BitmapResources.delete)), 30, 30));
                        i++;
                    }                    
                }

                for (int j = 0; j < rule_array.Count; j++)
                {
                    if (rule_array[j] != null)
                    {
                        Canvas.SetTop((Text)rule_array[j], 0 + j * 30);
                        Canvas.SetLeft((Text)rule_array[j], 0);
                        canvas.Children.Add((Text)rule_array[j]);

                        Canvas.SetTop((SimpleButton)delete_array[j], 0 + j * 30);
                        Canvas.SetRight((SimpleButton)delete_array[j], 0);
                        canvas.Children.Add((SimpleButton)delete_array[j]);

                        ((SimpleButton)delete_array[j]).Click += new EventHandler(buttonDelete_Click);
                    }
                }

            }
            else
            {
                Text text_noRules = new Text(Resources.GetFont(Resources.FontResources.NinaB), "There are not irrigation rules");
                Canvas.SetTop(text_noRules, 0);
                Canvas.SetLeft(text_noRules, 0);
                canvas.Children.Add(text_noRules);
            }
            Image image_newrule = new Image(Resources.GetBitmap(Resources.BitmapResources.edit));
            image_newrule.HorizontalAlignment = HorizontalAlignment.Center;
            image_newrule.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_newrule = new SimpleButton(image_newrule, irrigationScheduleWindow.Width / 2, irrigationScheduleWindow.Height / 5);
            button_newrule.BackColor = Colors.White;
            Canvas.SetBottom(button_newrule, 0);
            Canvas.SetLeft(button_newrule, 0);
            canvas.Children.Add(button_newrule);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, irrigationScheduleWindow.Width / 2, irrigationScheduleWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_newrule.Click += new EventHandler(buttonNewRule_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

            irrigationScheduleWindow.Child = canvas;
            irrigationScheduleWindow.Visibility = Visibility.Visible;
        }

        void buttonNewRule_Click(object sender, EventArgs evt)
        {
            AddIrrigationRulesWindow airw = new AddIrrigationRulesWindow(this);
            irrigationScheduleWindow.Close();
        }        

        void buttonReturn_Click(object sender, EventArgs evt)
        {
            irrigationScheduleWindow.Close();
        }

        void buttonDelete_Click(object sender, EventArgs evt)
        {
            SimpleButton buttonPressed = (SimpleButton)sender;
            for (int i = 0; i < delete_array.Count  ; i++)
            {
                if (delete_array[i] == buttonPressed)
                {
                    Rule rule = irrigation_rules[i] as Rule;
                    string guid = rule.ID;
                    IrrigationBL.GetInstance().DeleteRule(guid);

                    break;
                }
            }
            irrigationScheduleWindow.Close();
            new IrrigationScheduleWindow();
            
        }

        
    }
}
